/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "ui设置视口.h"
#include <UI/布局.h>

#include "A_引擎/文件工程/配置.h"



C_设置视口* C_设置视口::g当前操作2D编辑视口 = nullptr;

static uint32	g网格线绘制属性 = 0;


static bool on_设置视口按键(C_Widget* self, const S_键盘& ks) {
	
	return false;
}

static E_事件是否传递 on_设置视口鼠标按下(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标中键) {
		
	}
	return E_事件是否传递::e_事件传递_终止;
}

static E_事件是否传递 on_设置视口鼠标放开(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标中键) {
		
	}
	return E_事件是否传递::e_事件传递_终止;
}

static E_事件是否传递 on_设置视口移动(C_Widget* self, const ivec4& value) {
	C_设置视口* box = (C_设置视口*)self;

	
	return E_事件是否传递::e_事件传递_终止;
}


static void on_页面切换(S_Props& prop) {
	C_设置视口* box = dynamic_cast<C_设置视口*>(prop.m_UI->m_部件->m_父部件);

	C_Widget* view;
	switch (f_prop_enum(box->m_设置类型)) {
		case 0: view = box->m_界面设置面板; break;
		case 1: view = box->m_操作设置面板; break;
		case 2: view = box->m_文件设置面板; break;
		case 3: view = box->m_编译设置面板; break;
		default: view = nullptr; break;
		//case 0: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_界面设置面板); break;
		//case 1: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_操作设置面板); break;
		//case 2: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_文件设置面板); break;
		//case 3: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_编译设置面板); break;
	}

	if (view) {
		f_ui_滚动框安装视口组件(box->mui_滚动框, view);
		f_布局_扩展视口纵向滚动(view->m_父部件, view, 0);
	}
	
	
	box->m_更新绘制属性 = true;
	box->mui_滚动框->m_更新绘制属性 = true;

}


static E_事件是否传递 on_视口鼠标滚轮(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->m_父部件) {
		f_布局_扩展视口纵向滚动(self->m_父部件, self, C_Widget::g_鼠标.lZ * 40);
	}
	self->m_更新绘制属性 = true;

	return E_事件是否传递::e_事件传递_继续;
}

static void on_视口滚动框布局(C_Widget* self, C_Widget** w, uint32 num) {
	//C_列表框* box = dynamic_cast<C_列表框*>(w[0]);
	//
	//uint32 itemNum = box->m_列表数据.size();
	//box->f_setH(itemNum * box->m_行高度);
	//box->f_setW(self->m_Size.x);
	//box->f_setSize(self->m_Size);

	//f_布局_扩展视口纵向滚动(self, w[0], C_Widget::g_鼠标.lZ * 40);
}

static void on_设置视口绘制(C_Widget* self, S_2D画布* 画布) {
	C_设置视口& box = *static_cast<C_设置视口*>(self);

}

static void on_设置视口布局(C_Widget* self, C_Widget** w, uint32 num) {
	C_设置视口* box = dynamic_cast<C_设置视口*>(self);

	f_VBOX布局({1,1}, self->m_Size, w, num);


	C_Widget* view = f_ui_get滚动框视口(box->mui_滚动框);
	view->m_扩展视口属性->m_大小 = self->m_Size;
	//view->m_扩展视口属性->m_大小.y -= box->mui_滚动框->m_Pos.y + 2;
	f_布局_扩展视口纵向滚动(view->m_父部件, view, 0);

	box->mui_滚动框->f_setH(self->m_Size.y);
}

static void on_设置视口变换修改(C_Widget* self) {
	C_设置视口& box = *static_cast<C_设置视口*>(self);



}

C_设置视口::C_设置视口(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	g当前操作2D编辑视口 = this;

	mf_布局 = on_设置视口布局;
	mf_绘制 = on_设置视口绘制;
	mf_变换 = on_设置视口变换修改;
	mf_鼠标移动 = on_设置视口移动;

	m_是否未绘制 = true;
	m_属性栏 = nullptr;


	m_设置类型 = f_alloc_EnumProp(nullptr, {
		{u"界面", u"界面"},
		{u"操作", u"点击"},
		{u"文件", u"文件"},
		{u"编译", u"芯片"},
	}, u"设置面板", 1);
	m_设置类型.m_私有 = true;
	m_设置类型.m_UI->m_部件 = this;
	m_设置类型.m_UI->m_Update = on_页面切换;


	mui_切换导航 = f_ui_创建枚举选择框(ctx, E_方向::e_横向);
	mui_切换导航->m_是否延展 = { true, false };
	mui_切换导航->f_setSize({ 25,25 });
	mui_切换导航->f_bind属性(m_设置类型);
	f_添加子组件(mui_切换导航);
	f_ui_set枚举选择框图标和文字是否绘制(mui_切换导航, true, true);


	
	m_界面设置面板 = new C_界面设置面板(ctx);
	m_操作设置面板 = new C_操作设置面板(ctx);
	m_文件设置面板 = new C_文件设置面板(ctx);
	m_编译设置面板 = new C_编译设置面板(ctx);
	mui_滚动框 = f_ui_创建滚动框(ctx, false, true, m_操作设置面板);
	mui_滚动框->m_是否延展 = { true, true };
	mui_滚动框->mf_鼠标滚轮 = nullptr;
	mui_滚动框->f_setSize({1024,512});
	//mui_滚动框->mf_布局Fun = on_视口滚动框布局;
	m_界面设置面板->mf_鼠标滚轮 = on_视口鼠标滚轮;
	m_操作设置面板->mf_鼠标滚轮 = on_视口鼠标滚轮;
	m_文件设置面板->mf_鼠标滚轮 = on_视口鼠标滚轮;
	m_编译设置面板->mf_鼠标滚轮 = on_视口鼠标滚轮;
	f_添加子组件(mui_滚动框);


	//f_布局_扩展视口纵向滚动(m_操作设置面板->m_父部件, m_操作设置面板, 0);
}

C_设置视口::~C_设置视口() {
	f_prop_Release(m_设置类型);
}

C_Widget* f_工作区页面_设置视口(S_UI渲染环境& ctx) {
	C_设置视口* ui设置视口 = new C_设置视口(ctx);

	auto* 滚动窗口 = f_ui_创建滚动框(ctx, true, true, ui设置视口);
	滚动窗口->mf_鼠标滚轮 = nullptr;

	return 滚动窗口;
}








C_界面设置面板::C_界面设置面板(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	m_WName = u"界面设置";

	f_setSize({ 512,512 });
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
}

C_界面设置面板::~C_界面设置面板() {
	
}


static void on_快捷键编辑框布局(C_Widget* self, C_Widget** w, uint32 num) {
	float32 高度 = 30;
	float32 框宽度 = self->m_Size.x - 64;
	vec2 项大小 = { 框宽度 - 12, 高度 };

	
	float32 y = self->m_扩展视口属性->m_中心.y;
	//float32 y = 0;
	float32 边宽度 = 4;
	vec2 框位置 = {60, 2};
	for (uint32 i = 0; i < num; ++i) {
		auto e = w[i]->f_get更新组件();

		float32 总高度 = 边宽度;
		for (uint32 k = 0; k < e.m_Num; ++k) {
			
			e.m_W[k]->f_setPos({ 边宽度, 总高度});
			e.m_W[k]->f_setSize(项大小);

			总高度 += 高度 + 边宽度;
		}

		w[i]->f_setPos(框位置 + vec2{0, y});
		w[i]->f_setSize({ 框宽度, 总高度 });

		框位置.y += 总高度 + 8;
	}

	self->f_setW(self->m_Size.x);
	//self->f_setH(框位置.y + 20);
	self->f_setH(DEF_Max(框位置.y, self->m_父部件->m_Size.y));


	//f_widget_单个部件裁剪(self, { self->m_Pos.x, self->m_Pos.y, self->m_父部件->m_Size.x, self->m_父部件->m_Size.y }, self->m_父部件->m_GlobalLoc);
	f_widget_单个部件裁剪(self, { 0, 0, self->m_父部件->m_Size.x, self->m_父部件->m_Size.y }, self->m_父部件->m_GlobalLoc);
}

static void on_操作设置视口绘制(C_Widget* self, S_2D画布* 画布) {
	C_操作设置面板* box = (C_操作设置面板*)self;

	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	//图元 = {};
	//图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	
}

static void on_操作设置视口变换(C_Widget* self) {
	C_操作设置面板* box = (C_操作设置面板*)self;

	float32 y = self->m_扩展视口属性->m_中心.y;

	S_2D_PanelAttr 属性 = { {1,1}, {0,0}, 1, 3 };
	f_vg_set属性(self->m_画布, box->m_背景绘制.m_属性, 属性);


	auto e = self->f_get更新组件();

	box->m_背景绘制.f_alloc(e.m_Num + 1);
	
	for (uint32 i = 0; i < e.m_Num; ++i) {
		uint32 sn = e.m_W[i]->f_get子部件数量();
		mat3X2 mat;
		mat.row0 = e.m_W[i]->m_Pos;
		mat.row1 = e.m_W[i]->m_Size;
		mat.row2 = {3, 0};
		box->m_背景绘制.f_set单色填充(i+1, mat, S_UI主题::uic_面板背景, S_Widget预设凸包图元::m_圆角矩形);
	}

	mat3X2 mat;
	mat.row0 = self->m_Pos;
	mat.row1 = self->m_Size;
	mat.row2 = { 3, 0 };
	box->m_背景绘制.f_set单色填充(0, mat, S_UI主题::uic_面板背景 - S_RGBA8UI{30, 30, 30, 0}, S_Widget预设凸包图元::m_矩形);
}

C_操作设置面板::C_操作设置面板(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	m_WName = u"操作设置";

	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
	f_setSize({ 1024, 1024 });

	C_Widget* 全局键 = f_ui_创建组件();
	C_Widget* 视口键 = f_ui_创建组件();
	C_Widget* 节点键 = f_ui_创建组件();

	f_添加子组件(全局键);
	f_添加子组件(视口键);
	f_添加子组件(节点键);

	for (auto& e : C_键位映射配置::g_全局键位映射容器) {
		auto* key1 = f_ui_创建快捷键编辑框(ctx);
		全局键->f_添加子组件(key1);

		f_ui_快捷键编辑框绑定属性(key1, e.second);
	}

	for (auto& e : C_键位映射配置::g_3D视口键位映射容器) {
		auto* key1 = f_ui_创建快捷键编辑框(ctx);
		视口键->f_添加子组件(key1);

		f_ui_快捷键编辑框绑定属性(key1, e.second);
	}

	for (auto& e : C_键位映射配置::g_节点视口键位映射容器) {
		auto* key1 = f_ui_创建快捷键编辑框(ctx);
		节点键->f_添加子组件(key1);

		f_ui_快捷键编辑框绑定属性(key1, e.second);
	}


	mf_布局 = on_快捷键编辑框布局;
	mf_绘制 = on_操作设置视口绘制;
	mf_变换 = on_操作设置视口变换;
}

C_操作设置面板::~C_操作设置面板() {
}




C_编译设置面板::C_编译设置面板(S_UI渲染环境& ctx) : C_Widget(&ctx) {
	m_WName = u"编译设置";

	f_setSize({ 512,512 });
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
}

C_编译设置面板::~C_编译设置面板() {

}



